// pages/order/order.js
const units = require("../../utils/util.js")
import Dialog from '@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartTotal: {},
    cartList: [],
    checked: false,
    editsun: false
  },

  // 获取数据
  cartIndex() {
    units.axios({
      url: "cart/index",
      header: {
        "X-Byesame-Token": wx.getStorageSync('token')
      }
    }).then((res) => {
      this.setData({
        cartTotal: res.data.data.cartTotal,
        cartList: res.data.data.cartList
      })
      this.Judgmentfun()
    })
  },

  // 判断是否选中
  Judgmentfun() {
    let ids0 = this.data.cartList.filter(item => !item.checked).map(item => item.productId)
    let ids1 = this.data.cartList.filter(item => item.checked).map(item => item.productId)
    if (ids0.length == 0) {
      this.setData({
        checked: true
      })
    } else {
      this.setData({
        checked: false
      })
      this.cartchecked(0, ids0)
    }
    if (ids1.length != 0) {
      this.cartchecked(1, ids1)
    }
  },

  // 选择请求
  cartchecked(isChecked, productIds) {
    units.axios({
      url: "cart/checked",
      data: {
        isChecked,
        productIds
      },
      method: "POST",
      header: {
        "X-Byesame-Token": wx.getStorageSync('token')
      }
    }).then((res) => {
      this.setData({
        cartTotal: res.data.data.cartTotal,
        cartList: res.data.data.cartList
      })
    })
  },

  // 单选
  onChange(e) {
    this.setData({
      [`cartList[${e.currentTarget.dataset.index}].checked`]: e.detail
    })
    this.Judgmentfun()
  },

  // 全选
  onChangefun(e) {
    for (let index = 0; index < this.data.cartList.length; index++) {
      this.setData({
        [`cartList[${index}].checked`]: e.detail
      })
    }
    this.Judgmentfun()
  },

  // 切换编辑
  editsunfun() {
    this.setData({
      editsun: !this.data.editsun
    })
  },

  // 数量
  onChangesun(e) {
    let index = e.currentTarget.dataset.index
    units.axios({
      url: "cart/update",
      data: {
        goodsId: this.data.cartList[index].goodsId,
        id: this.data.cartList[index].id,
        number: e.detail,
        productId: this.data.cartList[index].productId
      },
      method: "POST",
      header: {
        "X-Byesame-Token": wx.getStorageSync('token')
      }
    }).then((res) => {
      if (res.statusCode == 200) {
        if (res.data.errno == 0) {
          this.cartIndex()
        } else {
          wx.showToast({
            title: res.data.errmsg,
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },

  // 下栏按钮
  onClickButton() {
    if (this.data.editsun) {
      let ids1 = this.data.cartList.filter(item => item.checked).map(item => item.productId)
      this.deletefun(ids1)
    } else {
      wx.navigateTo({
        url:"/pages/orderCheckout/orderCheckout?id=0"
      })
    }
  },

  // 删除弹框
  deletefun(e) {
    Dialog.confirm({
        message: '确定删除所选商品吗',
        cancelButtonText: "再想想"
      })
      .then(() => {
        this.cartdelete((e.currentTarget ? [e.currentTarget.dataset.id] : e))
      })
      .catch(() => {

      });
  },

  // 删除请求
  cartdelete(ids) {
    units.axios({
      url: "cart/delete",
      data: {
        productIds: ids
      },
      method: "POST",
      header: {
        "X-Byesame-Token": wx.getStorageSync('token')
      }
    }).then((res) => {
      if (res.statusCode == 200) {
        this.cartIndex()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!wx.getStorageSync('token')) {
      wx.redirectTo({
        url: "/pages/login/login"
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
		this.cartIndex()
		units.getitime()
		units.getendtime()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})